﻿//Joystick9 Harmonious Vigor
//version: creation

using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Logic.POI;

namespace HarmoniousVigor
{
    public static class Utils
    {
        static LocalPlayer Me;
        public static WoWPlayer GetHealTarget(int healCheckRange)
        {
            WoWPlayer tempPlayer = (from p in ObjectManager.GetObjectsOfType<WoWPlayer>(true, true)
                                    where p.IsFriendly && p.IsAlive && p.IsPlayer && p.Distance2D <= healCheckRange
                                    orderby p.HealthPercent ascending
                                    select p).FirstOrDefault();
            return tempPlayer;
        }
        public static int GetEnemiesThisDistanceMe(int distance)
        {
            return ObjectManager.GetObjectsOfType<WoWUnit>(true, false).Count(unit => !unit.IsFriendly && unit.Distance2D <= distance && unit.Combat && unit.IsAlive);
        }
        public static bool CastTarget(String spellName, WoWUnit target)
        {
            if (target != null && SpellManager.CanCast(spellName, target))
            {
                Log("Casting " + spellName + " on target");
                SpellManager.Cast(spellName, target);
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool CastMe(String spellName)
        {
            if (SpellManager.CanCast(spellName, Me))
            {
                Log("Casting " + spellName + " on me");
                SpellManager.Cast(spellName, Me);
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool BuffMe(String spellName)
        {
            if (SpellManager.CanCast(spellName, Me))
            {
                BuffLog("Buffing " + spellName + " on me");
                SpellManager.Cast(spellName, Me);
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool BuffTarget(String spellName, WoWUnit target)
        {
            if (target != null && SpellManager.CanCast(spellName, target))
            {
                BuffLog("Buffing " + spellName + " on target");
                SpellManager.Cast(spellName, target);
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool InterruptTarget(String spellName, WoWUnit target)
        {
            if (target != null && SpellManager.CanCast(spellName, target))
            {
                InterruptLog("Interrupting with " + spellName + " on target");
                SpellManager.Cast(spellName, target);
                return true;
            }
            else
            {
                return false;
            }
        }
        public static void Log(String message, params object[] args)
        {
            Logging.Write(Color.Yellow, "[HarmoniousVigor " + Me.Class.ToString() + "] " + message, args);
        }
        public static void ULog(String message, params object[] args)
        {
            Logging.Write(Color.LightBlue, "[HarmoniousVigor " + Me.Class.ToString() + "] " + message, args);
        }
        public static void BuffLog(String message, params object[] args)
        {
            Logging.Write(Color.Red, "[HarmoniousVigor " + Me.Class.ToString() + "] " + message, args);
        }
        public static void DLog(String message, params object[] args)
        {
            Logging.WriteDebug(Color.Green, "[HarmoniousVigor " + Me.Class.ToString() + "] " + message, args);
        }
        public static void InterruptLog(String message, params object[] args)
        {
            Logging.Write(Color.LimeGreen, "[HarmoniousVigor " + Me.Class.ToString() + "] " + message, args);
        }
        public static void setMe(LocalPlayer me)
        {
            Me = me;
        }
        public static void nullCheckCharacterSettings()
        {
            if (HarmoniousVigor.CharacterSettings == null)
            {
                switch (Me.Class)
                {
                    case WoWClass.DeathKnight: HarmoniousVigor.CharacterSettings = new DeathKnightSettings(); break;
                    case WoWClass.Druid: HarmoniousVigor.CharacterSettings = new DruidSettings(); break;
                    case WoWClass.Hunter: HarmoniousVigor.CharacterSettings = new HunterSettings(); break;
                    case WoWClass.Mage: HarmoniousVigor.CharacterSettings = new MageSettings(); break;
                    case WoWClass.Paladin: HarmoniousVigor.CharacterSettings = new PaladinSettings(); break;
                    case WoWClass.Priest: HarmoniousVigor.CharacterSettings = new PriestSettings(); break;
                    case WoWClass.Rogue: HarmoniousVigor.CharacterSettings = new RogueSettings(); break;
                    case WoWClass.Shaman: HarmoniousVigor.CharacterSettings = new ShamanSettings(); break;
                    case WoWClass.Warlock: HarmoniousVigor.CharacterSettings = new WarlockSettings(); break;
                    case WoWClass.Warrior: HarmoniousVigor.CharacterSettings = new WarriorSettings(); break;
                }
                HarmoniousVigor.CharacterSettings.Load();
            }
        }
        public static void nullCheckSettings()
        {
            if (HarmoniousVigor.Settings == null)
            {
                HarmoniousVigor.Settings = new HarmoniousVigorSettings();
                HarmoniousVigor.Settings.Load();
            }
        }
        public static void nullCheckForm()
        {
            if (HarmoniousVigor.Form == null)
            {
                HarmoniousVigor.Form = new HarmoniousVigorForm();
            }
        }

        public static int WillChainEnemyDistanceFrom(int yards, WoWUnit target)
        {
            return ObjectManager.GetObjectsOfType<WoWUnit>(true, false).Count(unit => unit.Location.Distance2D(target.Location) < yards
                    && unit.IsAlive && !unit.IsFriendly);
        }
        public static int WillChainFriendlyDistanceFrom(int yards, WoWUnit target)
        {
            return ObjectManager.GetObjectsOfType<WoWUnit>(true, false).Count(unit => unit.Location.Distance2D(target.Location) < yards
                    && unit.IsAlive && unit.IsFriendly);
        }
    }
}
